package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zb.dto.*;
import com.zb.entity.TbQuotaModel;
import com.zb.influx.InfluxReposityory;
import com.zb.mapper.TbQuotaMapper;
import com.zb.service.TbQuotaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName TbQuotaServiceImpl
 * @Description 服务接口实现
 * @Author xm
 * @Date 2025/03/25 11:30
 **/
@Service
public class TbQuotaServiceImpl extends ServiceImpl<TbQuotaMapper, TbQuotaModel> implements TbQuotaService {

    @Autowired
    private InfluxReposityory influxReposityory;
    /**
     * {"sn":"1001", "temp":56.5, "humidity":50}
     */
    @Override
    public DeviceInfoDTO analysis(String topic, Map<String, Object> payloadMap) {
        //根据主题查询指标
        LambdaQueryWrapper<TbQuotaModel> qw = new LambdaQueryWrapper<>();
        qw.eq(TbQuotaModel::getSubject, topic);
        List<TbQuotaModel> list = super.list(qw);
        if (list == null && list.size() <= 0) {
            return null;
        }
        //获取设备的编号key, device/status snKey=sn   feng/status snKey=snkey
        String snKey = list.get(0).getSnKey();
        //获取设备的编号的值
        String deviceId = payloadMap.get(snKey).toString();
        //返回数据模型中的设备对象
        DeviceDTO device = new DeviceDTO();
        device.setDeviceId(deviceId);
        //返回数据模型中的指标集合
        List<QuotaDTO> quotaList = Lists.newArrayList();
        //解析报文中的指标
        for (TbQuotaModel quota : list) {
            //获取指标数据的key quotaValueKey=temp  quotaValueKey = humidity
            String quotaValueKey = quota.getValueKey();
            //检查数据库中存在的数据key 是否在上报的数据中存在记录
            if (payloadMap.containsKey(quotaValueKey)){
                QuotaDTO quotaDTO = new QuotaDTO();
                //参数1 数据库中有记录的 参数2 返回json中存储的对象 没有记录
                BeanUtils.copyProperties(quota,quotaDTO);
                quotaDTO.setQuotaName(quota.getName());
                quotaDTO.setDeviceId(deviceId);
                //处理上报的数据并储存
                if ("String".equals(quotaDTO.getValueType())
                        || "Boolean".equals(quotaDTO.getValueType())) {
                    quotaDTO.setStringValue(payloadMap.get(quotaValueKey).toString());
                    quotaDTO.setValue(0.0);
                }else{
                    double value = Double.parseDouble(payloadMap.get(quotaValueKey).toString());
                    quotaDTO.setValue(value);
                    quotaDTO.setStringValue(payloadMap.get(quotaValueKey).toString());
                }
                quotaList.add(quotaDTO);
            }
        }
        DeviceInfoDTO deviceInfoDTO = new DeviceInfoDTO();
        deviceInfoDTO.setDevice(device);
        deviceInfoDTO.setQuotaList(quotaList);

        return deviceInfoDTO;
    }

    @Override
    public void saveQuotaToInflux(List<QuotaDTO> quotaList) {
        for (QuotaDTO quotaDTO : quotaList) {
            QuotaInfo quotaInfo = new QuotaInfo();
            BeanUtils.copyProperties(quotaDTO,quotaInfo);
            quotaInfo.setQuotaId(quotaDTO.getId().toString());
            influxReposityory.add(quotaInfo);
        }
    }

    @Override
    public List<QuotaAllInfo> getLastQuotaList(String deviceId) {
        String ql = "select last(value), * from quota where deviceId= '" + deviceId + "' group by quotaId";
        return influxReposityory.query(ql, QuotaAllInfo.class);
    }
}
